package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;

    /**
     * 新增检查项
     *
     * @param checkItem
     */
    @Override
    public void add(CheckItem checkItem) {
        checkItemMapper.add(checkItem);
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        //定义当前页吗和页面大小
        PageHelper.startPage(currentPage, pageSize);
        //执行查询
        List<CheckItem> checkItemList = checkItemMapper.findByCondition(queryString);
        //将查询的数据给pageInfo进行包装
        PageInfo<CheckItem> pageInfo = new PageInfo<>(checkItemList);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 删除检查项
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        //通过检查项的id查询是否被中间表引用
        Integer count =checkItemMapper.findCountByCheckItemId(id);
        if(count>0){
            throw new RuntimeException(MessageConstant.CHECKITEM_IS_QUOTED);
        }
        //通过id删除检查项
        checkItemMapper.deleteById(id);
    }

    @Override
    public CheckItem findById(Integer id) {

        return  checkItemMapper.findById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemMapper.edit(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemMapper.findAll();
    }

    /**
     * 通过检查组的id查询检查项的id
     * @param id
     * @return
     */
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkItemMapper.findCheckItemIdsByCheckGroupId(id);
    }


}
